package sxd.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import sxd.biz.SxdBiz;
import sxd.pojo.Dept;
import sxd.pojo.User;

/**
 * @author darren
 * @date 2016年9月8日 下午1:05:36
*/
@Controller
@RequestMapping("/sxd")
public class SxdController {
	
	public static Logger LOG = LoggerFactory.getLogger(SxdController.class);
	@Resource
	public SxdBiz biz;

	/**
	 * 主页
	 * @param request
	 * @return
	 */
	@GetMapping
	public String index(HttpServletRequest request){
		return index(request, 1);
	}
	/**
	 * 主页分页
	 * @param request
	 * @param page
	 * @return
	 */
	@GetMapping("/{page}")
	public String index(HttpServletRequest request,@PathVariable Integer page){
		request.setAttribute("deptList", biz.getDeptList());
		request.setAttribute("userPage", biz.getUserPage(10, page));
		return "index";
	}
	
	/**
	 * ajax获取部门列表
	 * @return
	 */
	@ResponseBody
	@GetMapping(value="/dept/list",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public List<Dept> deptList(){
		return biz.getDeptList();
	}
	/**
	 * ajax增加部门
	 * @param name
	 * @return
	 */
	@ResponseBody
	@PostMapping(value="/dept/add",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public JSONObject addDept(@RequestParam String name){
		JSONObject json = new JSONObject();
		Dept dept = new Dept();
		dept.setName(name);
		try {
			biz.addDept(dept);
			json.put("msg", "添加部门成功");
			json.put("err", 0);
		} catch (Exception e) {
			LOG.error("添加部门失败：{}",e.getMessage());
			json.put("msg", "添加部门失败");
			json.put("err", 1);
		}
		return json;
	}
	/**
	 * ajax删除部门
	 * @param id
	 * @return
	 */
	@ResponseBody
	@PostMapping(value="/dept/del/{id}",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public JSONObject delDept(@PathVariable Integer id){
		JSONObject json = new JSONObject();
		json.put("err", 1);
		if(biz.getUserCountByDept(id)==0){
			try {
				biz.deleteDept(id);
				json.put("msg", "删除部门成功");
				json.put("err", 0);
			} catch (Exception e) {
				LOG.error("添加部门失败：{}",e.getMessage());
				json.put("msg", "删除部门失败");
			}
		}else{
			json.put("msg", "该部门下有员工不允许删除");
		}
		return json;
	}
	/**
	 * ajax添加用户
	 * @param user
	 * @return
	 */
	@ResponseBody
	@PostMapping(value="/user/add",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public JSONObject addUser(@ModelAttribute("user") User user){
		JSONObject json = new JSONObject();
		try {
			biz.addUser(user);
			json.put("msg", "添加员工信息成功");
			json.put("err", 0);
		} catch (Exception e) {
			LOG.error("添加部门失败：{}",e.getMessage());
			json.put("msg", "添加员工信息失败");
			json.put("err", 1);
		}
		return json;
	}
	/**
	 * ajax 删除员工
	 * @param uid
	 * @return
	 */
	@ResponseBody
	@PostMapping(value="/user/del/{uid}",produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public JSONObject delUser(@PathVariable Integer uid){
		JSONObject json = new JSONObject();
		try {
			biz.deleteUser(uid);
			json.put("msg", "删除员工成功");
			json.put("err", 0);
		} catch (Exception e) {
			LOG.error("添加部门失败：{}",e.getMessage());
			json.put("msg", "删除员工失败");
			json.put("err", 1);
		}
		return json;
	}
	
	@InitBinder("user")
	public void initUserBinder(WebDataBinder binder){
		binder.setFieldDefaultPrefix("u.");
	}
}
